
window.indexedDB=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB||window.msIndexedDB;
window.IDBTransaction=window.IDBTransaction||window.webkitTransaction||window.msTransaction;
window.IDBKeyRange=window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange;
window.IDBCursor=window.IDBCursor||window.webkitIDBCursor||window.msIDBCursor;
var dbName='Data';
var dbVersion='2017';
var idb,datatable;
var data;
function window_onload() {             //页面加载时执行
    var dbConnect=window.indexedDB.open(dbName,dbVersion);
    dbConnect.onsuccess=function (e) {
        idb = e.target.result;
        datatable = document.getElementById("datatable");
        showAllData(true);
        console.log("显示全部留言内容");
    };
    dbConnect.onerror=function () {
        alert("连接数据库失败");
    };
    dbConnect.onupgradeneeded=function (e) {
        idb = e.target.result;
        var storename='message';
        var optionalParameters={
            keyPah:'id',
            autoIncrement:true                           //id自动编号
        };
        var store = idb.createObjectStore(storename, optionalParameters);
        console.log('创建对象仓库成功');
        var indexname='nameIndex';
        var keyPath='name';
        var optionalParameters={
            unique:true,
            multiEntry:false
        };
        var idx=store.createIndex(indexname,keyPath,optionalParameters);//创建索引
        alert('创建索引成功');
    };
}
function btnAdd_onclick() {//单击发布留言按钮时执行
    data = new Object();
    var now =new Date();
    data.Name=document.getElementById("nickName").value;
    data.Date=String(1900+now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
    data.Word=document.getElementById("theMessage").value;
    var tx=idb.transaction(['message'],"readwrite");

    var chkErrorMsg="";
    tx.oncomplete=function () {          //追加数据成功后执行
        if(chkErrorMsg!="")
            alert(chkErrorMsg);
        else{
            alert('留言成功');
            showAllData(false);
            btnClear_onclick();
        }
    }
    tx.onabort=function () {
        alert('留言失败');
    }
    var store=tx.objectStore('message');
    var idx=store.index('nameIndex');
    var range = IDBKeyRange.only(data.Name);
    var direction="next";
    var req =idx.openCursor(range,direction);
    req.onsuccess=function () {
        var cursor=this.result;
        if(cursor){
            chkErrorMsg="输入的昵称在数据库中已经存在！";
        }
        else{
            var value ={
                name:data.Name,
                date:data.Date,
                word:data.Word
            };
            store.put(value);
        }
    }
    req.onerror=function () {
        console.log('留言失败');
    }
}
function btnClear_onclick() {
    document.getElementById("nickName").value="";
    document.getElementById("theMessage").value="";
}
function showAllData(loadPage) {
    if(!loadPage)
        removeAllData();
    var tx=idb.transaction(['message'],"readonly");//开启事务
    var store=tx.objectStore('message');
    var idx=store.index('nameIndex');
    var range = IDBKeyRange.lowerBound(1);
    var direction="next";
    var req =idx.openCursor(range,direction);
    var i=0;
    req.onsuccess=function () {
        var cursor = this.result;
        if(cursor){
            i+=1;
            showData(cursor.value,i);
            cursor.continue();
        }
    }
    req.onerror=function () {
        alert('检索数据失败');
    }
}
function removeAllData() {
    for(var i=datatable.childNodes.length-1;i>1;i--)
        datatable.removeChild(datatable.childNodes[i]);
}
function tr_onclick(tr,i) {
    document.getElementById("nickName").value=tr.children.item(0).innerHTML;
    document.getElementById("theMessage").value=tr.children.item(1).innerHTML;
    document.getElementById("btnAdd").disabled="disabled";
}
function showData(row,i) {
    var tr=document.createElement('tr');
    tr.setAttribute("onclick","tr_onclick(this,"+i+")");
    var td1=document.createElement('td');
    td1.innerHTML=row.name;
    var td2=document.createElement('td');
    td2.innerHTML=row.word;
    var td3=document.createElement('td');
    td3.innerHTML=row.date;
    tr.appendChild(td1);
    tr.appendChild(td2);
    tr.appendChild(td3);
    datatable.appendChild(tr);
}
//删除订单
function btnDelete_onclick() {
    var tx = idb.transaction(['message'], "readwrite");
    tx.oncomplete = function () {       //删除成功之后触发
        alert("删除数据成功");
        showAllData(false);
    }
    tx.onabort = function () {
        alert('删除数据失败');
    }
    var store = tx.objectStore('message');
    var idx = store.index('nameIndex');
    var range = IDBKeyRange.only(document.getElementById("nickName").value);
    var direction = "next";
    var req = idx.openCursor(range, direction);
    req.onsuccess = function () {
        var cursor = this.result;
        if (cursor) {
            cursor.delete();
        }
    }
    req.onerror = function () {
        alert('删除数据失败');
    }
}